package com.kotlin.flong.coroutines

import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * @Author:liangjl
 * @Date: 2020/7/7-17:46
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 1、使用 finally来关闭资源
 * 2、join与cancelAndJoin都会等待所有清理动作完成才会继续往下执行
 * 3、kotlin协程取消之前会执行finally
 */

fun main() = runBlocking {

    var job = launch {

        try {
            repeat(100) { i ->
                println("job :i am sleeping $i")
                delay(500)
            }
        } finally {
            println("finally 执行了")
        }
    }

    delay(1300)

    println("Hello World")

    job.cancelAndJoin()

    println("Welcome")
}